import abc
from dataclasses import dataclass

from markitdown import MarkItDown

from src.markitdown_api.app.models.task import Task


@dataclass
class TaskResult:
    content: str
    title: str | None = None


class TaskHandler:

    @abc.abstractmethod
    def handle(self, task: Task) -> TaskResult:
        pass


# 默认处理器使用 markitdown
class DefaultHandler(TaskHandler):

    def handle(self, task: Task) -> TaskResult:
        md = MarkItDown(enable_plugins=True)
        result = md.convert(task.input_path)
        return TaskResult(
            title=result.title,
            content=result.text_content
        )

default_handler = DefaultHandler()